package cn.kgc.service.impl;

import cn.kgc.dao.Lxdetail2Dao;
import cn.kgc.dao.LxdetailDao;
import cn.kgc.dao.LxpayDao;
import cn.kgc.pojo.Lxdetail;
import cn.kgc.pojo.Lxdetail2;
import cn.kgc.pojo.Lxpay;
import cn.kgc.service.LxpayService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: 木子
 * @date: 2022/2/16
 * @description:
 */
@Service
public class LxpayServiceImpl implements LxpayService {

    @Resource
    private LxpayDao ly;

    @Resource
    private LxdetailDao lt;

    @Resource
    private Lxdetail2Dao ld;

    @Override
    public List<Lxpay> selAll() {
        return ly.selAll();
    }

    /**
     * 新增总订单
     *
     * @return
     */
    @Override
    @Transactional
    public int newAdd() {

        try {
            //获得总人数
            int i = ly.selPdm();
            //订单编号
            long timeMillis = System.currentTimeMillis();
            String code = "HS" + timeMillis;
            //获得总基础金额
            double v = ly.selParroll();

            Lxpay lxpay = new Lxpay();
            lxpay.setCount(i);
            lxpay.setCoder(code);
            lxpay.setTotal(v);
            ly.add(lxpay);

            //System.out.println("主键id"+lxpay.getId());
            //添加二级订单详情
            List<Lxdetail> list = lt.selAll();
            for (Lxdetail lxdetail : list) {
                lxdetail.setPid(lxpay.getId());
                lt.add(lxdetail);
                //add2(lxdetail.getId());
                List<Lxdetail2> s = ld.selById2(lxdetail.getId());
                for (Lxdetail2 t : s) {
                    t.setLid(lxdetail.getId());
                    t.setFid(lxpay.getId());
                    ld.addlx(t);
                }

                //System.out.println("add = " + add);

            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }

        return 1;
    }


    @Override
    public int selPdm() {
        return ly.selPdm();
    }

    @Override
    public int selParroll() {
        return 0;
    }

    @Override
    public List<Lxdetail> selById(int id) {
        return ly.selById(id);
    }
}
